<?php

use think\migration\Seeder;
use app\admin\model\AuthRule;
use app\admin\model\AuthGroup;
use app\admin\model\Member;
class AddDefaultAuthData extends Seeder
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {
        $rules = [
            [
                'controller' => 'Index',
                'action' => 'index',
                'title' => '控制台',
                'icon' => 'menu-icon fa fa-tachometer',
                'sort' => 10,
                'status' => 0,
                'children' => [],
            ], [
                'controller' => '',
                'action' => '',
                'title' => '网站内容',
                'icon' => 'menu-icon fa fa-desktop',
                'sort' => 20,
                'status' => 1,
                'children' => [
                    [
                        'controller' => 'HomeBanner',
                        'action' => 'index',
                        'title' => '首页Banner',
                        'sort' => 200,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'HomeBanner',
                                'action' => 'create',
                                'title' => '添加首页Banner',
                                'sort' => 201,
                            ], [
                                'controller' => 'HomeBanner',
                                'action' => 'update',
                                'title' => '更新首页Banner',
                                'sort' => 202,
                            ], [
                                'controller' => 'HomeBanner',
                                'action' => 'delete',
                                'title' => '删除首页Banner',
                                'sort' => 203,
                            ], [
                                'controller' => 'HomeBanner',
                                'action' => 'resort',
                                'title' => '首页Banner排序',
                                'sort' => 204,
                            ]
                        ],
                    ], [
                        'controller' => 'News',
                        'action' => 'index',
                        'title' => '新闻中心',
                        'sort' => 210,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'News',
                                'action' => 'create',
                                'title' => '添加新闻',
                                'sort' => 211,
                            ], [
                                'controller' => 'News',
                                'action' => 'update',
                                'title' => '更新新闻',
                                'sort' => 212,
                            ], [
                                'controller' => 'News',
                                'action' => 'delete',
                                'title' => '删除新闻',
                                'sort' => 213,
                            ], [
                                'controller' => 'News',
                                'action' => 'resort',
                                'title' => '新闻排序',
                                'sort' => 214,
                            ]
                        ],
                    ], [
                        'controller' => 'Notice',
                        'action' => 'index',
                        'title' => '通知通告',
                        'sort' => 220,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Notice',
                                'action' => 'create',
                                'title' => '添加通知通告',
                                'sort' => 221,
                            ], [
                                'controller' => 'Notice',
                                'action' => 'update',
                                'title' => '更新通知通告',
                                'sort' => 222,
                            ], [
                                'controller' => 'Notice',
                                'action' => 'delete',
                                'title' => '删除通知通告',
                                'sort' => 223,
                            ], [
                                'controller' => 'Notice',
                                'action' => 'resort',
                                'title' => '通知通告排序',
                                'sort' => 224,
                            ]
                        ],
                    ], [
                        'controller' => 'Column',
                        'action' => 'index',
                        'title' => '专题专栏',
                        'sort' => 230,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Column',
                                'action' => 'create',
                                'title' => '添加专题专栏',
                                'sort' => 231,
                            ], [
                                'controller' => 'Column',
                                'action' => 'update',
                                'title' => '更新专题专栏',
                                'sort' => 232,
                            ], [
                                'controller' => 'Column',
                                'action' => 'delete',
                                'title' => '删除专题专栏',
                                'sort' => 233,
                            ], [
                                'controller' => 'Column',
                                'action' => 'resort',
                                'title' => '专题专栏排序',
                                'sort' => 234,
                            ]
                        ],
                    ], [
                        'controller' => 'Research',
                        'action' => 'index',
                        'title' => '科学研究',
                        'sort' => 240,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Research',
                                'action' => 'create',
                                'title' => '添加科学研究',
                                'sort' => 241,
                            ], [
                                'controller' => 'Research',
                                'action' => 'update',
                                'title' => '更新科学研究',
                                'sort' => 242,
                            ], [
                                'controller' => 'Research',
                                'action' => 'delete',
                                'title' => '删除科学研究',
                                'sort' => 243,
                            ], [
                                'controller' => 'Research',
                                'action' => 'resort',
                                'title' => '科学研究排序',
                                'sort' => 244,
                            ]
                        ],
                    ], [
                        'controller' => 'Service',
                        'action' => 'index',
                        'title' => '社会服务',
                        'sort' => 250,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Service',
                                'action' => 'create',
                                'title' => '添加社会服务',
                                'sort' => 251,
                            ], [
                                'controller' => 'Service',
                                'action' => 'update',
                                'title' => '更新社会服务',
                                'sort' => 252,
                            ], [
                                'controller' => 'Service',
                                'action' => 'delete',
                                'title' => '删除社会服务',
                                'sort' => 253,
                            ], [
                                'controller' => 'Service',
                                'action' => 'resort',
                                'title' => '社会服务排序',
                                'sort' => 254,
                            ]
                        ],
                    ], [
                        'controller' => 'Apply',
                        'action' => 'index',
                        'title' => '项目申报',
                        'sort' => 260,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Apply',
                                'action' => 'read',
                                'title' => '查看项目申报',
                                'sort' => 261,
                            ], [
                                'controller' => 'Apply',
                                'action' => 'export',
                                'title' => '导出项目申报',
                                'sort' => 262,
                            ]
                        ],
                    ],  [
                        'controller' => 'Subscribe',
                        'action' => 'index',
                        'title' => '师资报名',
                        'sort' => 270,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Subscribe',
                                'action' => 'export',
                                'title' => '导出师资报名',
                                'sort' => 271,
                            ]
                        ],
                    ], [
                        'controller' => 'LinkBanner',
                        'action' => 'index',
                        'title' => '友情链接',
                        'sort' => 280,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'LinkBanner',
                                'action' => 'create',
                                'title' => '添加友情链接',
                                'sort' => 281,
                            ], [
                                'controller' => 'LinkBanner',
                                'action' => 'update',
                                'title' => '更新友情链接',
                                'sort' => 282,
                            ], [
                                'controller' => 'LinkBanner',
                                'action' => 'delete',
                                'title' => '删除友情链接',
                                'sort' => 283,
                            ], [
                                'controller' => 'LinkBanner',
                                'action' => 'resort',
                                'title' => '友情链接排序',
                                'sort' => 284,
                            ]
                        ],
                    ], [
                        'controller' => 'SmsLog',
                        'action' => 'index',
                        'title' => '短信日志',
                        'sort' => 290,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'SmsLog',
                                'action' => 'read',
                                'title' => '查看短信日志',
                                'sort' => 291,
                            ], [
                                'controller' => 'SmsLog',
                                'action' => 'export',
                                'title' => '导出短信日志',
                                'sort' => 292,
                            ]
                        ],
                    ],
                ],
            ], [
                'controller' => '',
                'action' => '',
                'title' => '基础数据',
                'icon' => 'menu-icon fa fa-hdd-o',
                'sort' => 30,
                'status' => 1,
                'children' => [
                    [
                        'controller' => 'CategoryRoot',
                        'action' => 'index',
                        'title' => '栏目管理',
                        'sort' => 310,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'CategoryRoot',
                                'action' => 'update',
                                'title' => '更新栏目',
                                'sort' => 313,
                            ]
                        ],
                    ], [
                        'controller' => 'Category',
                        'action' => 'index',
                        'title' => '栏目分类',
                        'sort' => 320,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Category',
                                'action' => 'create',
                                'title' => '添加栏目分类',
                                'sort' => 321,
                            ], [
                                'controller' => 'Category',
                                'action' => 'update',
                                'title' => '更新栏目分类',
                                'sort' => 322,
                            ], [
                                'controller' => 'Category',
                                'action' => 'delete',
                                'title' => '删除栏目分类',
                                'sort' => 323,
                            ], [
                                'controller' => 'Category',
                                'action' => 'resort',
                                'title' => '栏目分类排序',
                                'sort' => 324,
                            ]
                        ],
                    ]
                ],
            ], [
                'controller' => '',
                'action' => '',
                'title' => '用户及组',
                'icon' => 'menu-icon fa fa-users',
                'sort' => 40,
                'status' => 1,
                'children' => [
                    [
                        'controller' => 'User',
                        'action' => 'index',
                        'title' => '注册用户管理',
                        'sort' => 410,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'User',
                                'action' => 'lock',
                                'title' => '冻结用户账号',
                                'sort' => 411,
                            ], [
                                'controller' => 'User',
                                'action' => 'unlock',
                                'title' => '解冻用户账号',
                                'sort' => 412,
                            ], [
                                'controller' => 'User',
                                'action' => 'export',
                                'title' => '导出用户信息',
                                'sort' => 413,
                            ]
                        ],
                    ], [
                        'controller' => 'Member',
                        'action' => 'index',
                        'title' => '管理员管理',
                        'sort' => 420,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'Member',
                                'action' => 'read',
                                'title' => '添加管理员',
                                'sort' => 421,
                            ], [
                                'controller' => 'Member',
                                'action' => 'create',
                                'title' => '添加管理员',
                                'sort' => 422,
                            ], [
                                'controller' => 'Member',
                                'action' => 'update',
                                'title' => '更新管理员',
                                'sort' => 423,
                            ], [
                                'controller' => 'Member',
                                'action' => 'delete',
                                'title' => '删除管理员',
                                'sort' => 424,
                            ]
                        ],
                    ], [
                        'controller' => 'AuthGroup',
                        'action' => 'index',
                        'title' => '管理员组管理',
                        'sort' => 430,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'AuthGroup',
                                'action' => 'create',
                                'title' => '添加管理员组',
                                'sort' => 431,
                            ], [
                                'controller' => 'AuthGroup',
                                'action' => 'update',
                                'title' => '更新管理员组',
                                'sort' => 432,
                            ], [
                                'controller' => 'AuthGroup',
                                'action' => 'delete',
                                'title' => '删除管理员组',
                                'sort' => 433,
                            ], [
                                'controller' => 'AuthGroup',
                                'action' => 'resort',
                                'title' => '管理员组排序',
                                'sort' => 434,
                            ]
                        ],
                    ]
                ],
            ], [
                'controller' => '',
                'action' => '',
                'title' => '系统设置',
                'icon' => 'menu-icon fa fa-cog',
                'sort' => 50,
                'status' => 1,
                'children' => [
                    [
                        'controller' => 'Setting',
                        'action' => 'site',
                        'title' => '网站设置',
                        'sort' => 510,
                        'status' => 1,
                        'children' => [
                            // [
                            //     'controller' => 'Setting',
                            //     'action' => 'site',
                            //     'title' => '全站设置',
                            //     'sort' => 511,
                            //     'status' => 1,
                            // ], [
                            //     'controller' => 'Setting',
                            //     'action' => 'smtp',
                            //     'title' => '邮箱设置',
                            //     'sort' => 512,
                            //     'status' => 1,
                            // ], [
                            //     'controller' => 'Setting',
                            //     'action' => 'url',
                            //     'title' => 'URL模式',
                            //     'sort' => 513,
                            // ], [
                            //     'controller' => 'Setting',
                            //      'action' => 'active',
                            //     'title' => '激活邮件',
                            //     'sort' => 514,
                            // ], [
                            //     'controller' => 'Setting',
                            //     'action' => 'oauth',
                            //     'title' => '删除管理员',
                            //     'sort' => 515,
                            // ]
                        ],
                    ], [
                        'controller' => 'AuthRule',
                        'action' => 'index',
                        'title' => '后台菜单',
                        'sort' => 520,
                        'status' => 1,
                        'children' => [
                            [
                                'controller' => 'AuthRule',
                                'action' => 'create',
                                'title' => '添加后台菜单',
                                'sort' => 521,
                            ], [
                                'controller' => 'AuthRule',
                                'action' => 'update',
                                'title' => '更新后台菜单',
                                'sort' => 522,
                            ], [
                                'controller' => 'AuthRule',
                                'action' => 'delete',
                                'title' => '删除后台菜单',
                                'sort' => 523,
                            ], [
                                'controller' => 'AuthRule',
                                'action' => 'resort',
                                'title' => '后台菜单排序',
                                'sort' => 524,
                            ]
                        ],
                    ],
                ],
            ], [
                'name' => '',
                'controller' => '',
                'action' => '',
                'title' => '个人中心',
                'icon' => 'menu-icon fa fa-user',
                'sort' => 90,
                'status' => 1,
                'children' => [
                    [
                        'controller' => 'Personal',
                        'action' => 'update',
                        'title' => '个人资料',
                        'sort' => 91,
                        'status' => 1,
                    ], [
                        'controller' => 'Personal',
                        'action' => 'change_password',
                        'title' => '修改密码',
                        'sort' => 92,
                        'status' => 1,
                    ], [
                        'controller' => 'Login',
                        'action' => 'logout',
                        'title' => '退出',
                        'sort' => 93,
                        'status' => 1,
                    ],
                ],
            ],
        ];
        $this->sendRuleList($rules, 0);

        $groups = [
            [
                'title' => '超级管理员',
                'sort' => 0,
                'status' => 1,
                'is_super' => 1,
            ], [
                'title' => '管理员',
                'sort' => 10,
                'status' => 1,
                'is_super' => 0,
            ],
        ];
        $group_default = ['status' => 1, 'member_id' => 1];
        $GroupModel = new AuthGroup;
        $super_group_id = 0;
        foreach ($groups as $key => $group) {
            $res = $GroupModel->findOrCreateByTitle(array_merge($group_default, $group));
            if($res['status'] && $group['title'] == '超级管理员'){
                $super_group_id = $res['data']['id'];
            }
        }

        $admin = [
            'name' => 'admin',
            'password' => 'admin888',
            'confirm_password' => 'admin888',
            'nickname' => '超级管理员',
            'email' => 'admin@test.com',
            'group_id' => $super_group_id,
            'status' => 1,
        ];
        $MemberModel = new Member;
        $MemberModel->findOrCreate($admin);
    }

    public function sendRuleList($rules, $parent_id){
        $RuleModel = new AuthRule;
        $default_data = ['status' => 0, 'condition' => '', 'member_id' => 1, 'parent_id' => $parent_id];
        foreach($rules as $key => $rule){
            $res = $RuleModel->findOrCreate(array_merge($default_data, $rule));
            if(!$res['status']){
                // 添加或更新失败
                continue;
            }
            if(isset($rule['children'])){
                $res_data = $res['data'];
                if(!isset($res_data['id']) || $res_data['id'] < 1){
                    continue;
                }
                $this->sendRuleList($rule['children'], $res_data['id']);
            }
        }
    }
}